/*
 * Copyright 2014 JooKar (info@jookar.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.jookar.privatechat.bussiness;

import nl.jookar.privatechat.model.EmailAddress;
import nl.jookar.privatechat.model.User;
import nl.jookar.privatechat.utils.DataFailureException;

/**
 * TODO: Write type comment
 * 
 * @author Joost A. Bloemsma
 */
public class UserRegistry {
    static UserRegistry _registry = new UserRegistry();
    
    private UserRegistry() {
        super();
    }
    
    /**
     * Checks if the passed user is a valid, registered, user
     * @param address
     * @return true if the user is valid
     * @throws DataFailureException 
     */
    public static boolean isValidUser(EmailAddress address) throws DataFailureException {
        return _registry._isValidUser(address);
    }
    
    /**
     * Checks if the passed user is a valid, registered, user.
     * The EmailAddress property of the user must be set.
     * @param user
     * @return true if the user is valid
     * @throws DataFailureException 
     */
    public static boolean isValidUser(User user) throws DataFailureException {
        EmailAddress address = user.getEmailAddress();
        if (address == null) throw new UserWithoutAddressException();
        return isValidUser(address);
    }
    
    public static User getUser(EmailAddress address) throws UnknownUserException {
        return _registry._getUser(address); 
    }
    
    public String getUserName(EmailAddress address) {
        return _getUserName(address);
    }
    
    private boolean _isValidUser(EmailAddress address) {
        // TODO: To be implemented
        return true;
    }
    
    private User _getUser(EmailAddress address) throws UnknownUserException {
        // TODO: To be implemented
        
        if (!_isValidUser(address)) throw new UnknownUserException();
        
        User usr = new User();
        usr.setName(_getUserName(address));
        usr.setEmailAddress(address);
        return usr;
    }
    
    private String _getUserName(EmailAddress address) {
        return "Generated name for " + address.getAddress();
    }
}

